﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MediaManager
{
    public partial class ZhuankeSearchFrm : Form
    {
        DataClasses1DataContext _dataContext = new DataClasses1DataContext();
        private List<BaseTable> _listBasetable = null;
        public ZhuankeSearchFrm()
        {
            InitializeComponent();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnInit_Click(object sender, EventArgs e)
        {
            Init();
        }
        private void Init()
        {
            txtABIZuo.Text = "";
            txtABIYou.Text = "";
            cboXueGuan.SelectedIndex = -1;
            DateTime dt = DateTime.Now;
            dtbegin.Value = dt;
            dtend.Value = dt;
            txtZaoYingHaoMa.Text = "";
            cboZhiLiao.SelectedIndex = -1;
            txtShouShu.Text = "";
            cboBingQingTu.SelectedIndex = -1;
            txtSuiFang.Text = "";
            dataGridView1.DataSource = null;
            dataGridView1.Refresh();
        }

        private void ZhuankeSearchFrm_Load(object sender, EventArgs e)
        {
            Init();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                List<ZhuanKe> listzhuankeSystem = _dataContext.ZhuanKe.Where(p => true).ToList();
                bool b = false;
                if (listzhuankeSystem.Count > 0)
                {
                    if (this.txtABIZuo.Text.Length > 0)
                    {
                        b = true;
                        listzhuankeSystem = listzhuankeSystem.Where(p => p.ABI左.Contains(txtABIZuo.Text)).ToList();
                    }
                    if (txtABIYou.Text.Length > 0)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.ABI右.Contains(txtABIYou.Text)).ToList();
                        }
                    }
                    if (cboXueGuan.SelectedIndex > -1)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            switch (cboXueGuan.SelectedIndex)
                            {
                                case 0:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.血管造影 == 0).ToList();
                                        break;
                                    }
                                case 1:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.血管造影1 == 0).ToList();
                                        break;
                                    }
                                case 2:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.血管造影2 == 0).ToList();
                                        break;
                                    }
                            }
                        }
                    }
                    if (dtbegin.Value < dtend.Value)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.造影日期 >= dtbegin.Value && p.造影日期 < dtend.Value).ToList();
                        }
                    }
                    if (txtZaoYingHaoMa.Text.Length > 0)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.造影号码.Contains(txtZaoYingHaoMa.Text)).ToList();
                        }
                    }
                    if (cboZhiLiao.SelectedIndex > -1)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.治疗方式 == cboZhiLiao.SelectedIndex).ToList();
                        }
                    }
                    if (txtShouShu.Text.Length > 0)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.手术名称.Contains(txtShouShu.Text)).ToList();
                        }
                    }
                    if (cboBingQingTu.SelectedIndex > -1)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            switch (this.cboBingQingTu.SelectedIndex)
                            {
                                case 0:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.病情 == 0).ToList();
                                        break;
                                    }
                                case 1:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.病情1 == 0).ToList();
                                        break;
                                    }
                                case 2:
                                    {
                                        listzhuankeSystem = listzhuankeSystem.Where(p => p.病情2 == 0).ToList();
                                        break;
                                    }
                            }
                        }
                    }
                    if (txtSuiFang.Text.Length > 0)
                    {
                        b = true;
                        if (listzhuankeSystem.Count > 0)
                        {
                            listzhuankeSystem = listzhuankeSystem.Where(p => p.随访记录.Contains(txtSuiFang.Text)).ToList();
                        }
                    }
                }
                if (b && listzhuankeSystem.Count > 0)
                {
                    var result = from p in _dataContext.BaseTable.ToList()
                                 from q in listzhuankeSystem
                                 where p.编号 == q.id
                                 select p;
                    _listBasetable = result.ToList();
                    dataGridView1.DataSource = _listBasetable;
                    dataGridView1.Refresh();
                }
                else
                {
                    dataGridView1.DataSource = null;
                    dataGridView1.Refresh();
                    _listBasetable = null;
                }
            }
            catch (Exception ex)
            {
                GlobalBase.Global.Instance.ShowMessage(ex.Message);
                GlobalBase.CatchException.DealWithException(ex);
            }
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {
            Global.Instance.ShowFormID(dataGridView1, _listBasetable);
        }
    }
}
